Introduction to String Indexing in Python
In Python, a string is a sequence of characters arranged in a specific order. Each character has a fixed position, which is called its index.
For example, consider the string text = "HELLO WORLD". The position of each character is shown below:
| Character | H | E | L | L | O | W | O | R | L | D | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
Python follows zero-based indexing, which means:
- The first character starts at index 0
- The second character is at index 1
- This continues up to the last character in the string
In this example, every character in "HELLO WORLD" can be accessed using its index value.
Python Strings vs Numbers
Accessing Characters in a String Using Index
In Python, each character in a string can be accessed using its position, known as an index. The index starts at 0 for the first character and increases step by step up to the last character.
For example, in the string "HELLO WORLD":
- The character
'H'is at index 0 - The space after
'HELLO'is at index 5 - The final character
'D'is at index 10
To access a character, place the index inside square brackets after the string variable. This allows you to retrieve any specific character directly.
text = "HELLO WORLD"
print(text[0]) # First character
print(text[7]) # Eighth character
print(text[10]) # Last character
Output:
H
O
D
Explanation:
text[0]→ ‘H’text[7]→ ‘O’text[10]→ ‘D’
The string "HELLO WORLD" contains 11 characters, with index values ranging from 0 to 10. Indexing makes it easy to access individual characters without scanning the entire string.
Important Note: IndexError in String Indexing
If you try to access a position outside the valid range, Python raises an IndexError.
text = "HELLO WORLD"
print(text[20]) # Invalid index
Output:
IndexError: string index out of range
The index 20 is out of range because the string has only 11 characters (valid index range: 0–10). Python prevents invalid access by raising an error instead of returning incorrect data.
Pro Tip:
Indexes are useful, but it’s important to stay within the valid range. Python also provides a simpler way to access characters from the end of a string using negative indexing, which is covered in the next section.
Understanding Negative Indexing in Python
In Python strings, indexing does not only start from the beginning. Python also allows you to access characters from the end of a string using negative indexing.
With negative indexing:
-1refers to the last character-2refers to the second-last character- and so on, moving backward through the string
For example, in the string "HELLO WORLD", the character 'D' is at index -1, while the first character 'H' is at index -11.
This approach is especially useful when you want to access elements from the end without calculating the exact position using positive indices.
Example demonstrating negative indexing:
| Character | H | E | L | L | O | W | O | R | L | D | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Negative Index | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
phrase = "HELLO WORLD"
print(phrase[-1]) # Last character
print(phrase[-5]) # Fifth character from the end
print(phrase[-11]) # First character
Output:
D
W
H
Explanation:
phrase[-1]→ ‘D’ (last character)phrase[-5]→ ‘W’ (fifth from the end)phrase[-11]→ ‘H’ (first character)
Negative indexing provides a simple and reliable way to work with characters from the end of a string, without needing to calculate its length manually.
Note:
Negative indexing is particularly useful when working with dynamic data, where the length of the string may not be known in advance.
Real-World Example:
filename = "report.pdf"
print(filename[-3:]) # Get file extension
This is commonly used in file handling to extract extensions like “pdf”, “txt”, or “jpg”.
IndexError on Out-of-Range Negative Index
When using negative indexing in Python strings, it is important to stay within the valid range. If the index goes beyond the length of the string (too far left), Python raises an IndexError.
This type of error usually occurs when negative indexing is used without considering the actual length of the string.
phrase = "PYTHON SCRIPT"
print(phrase[-20]) # Invalid negative index
Output:
IndexError: string index out of range
The index -20 is outside the valid range because the string has fewer characters. In Python, valid negative indices start from -1 (last character) up to -len(string). Going beyond this range results in an error.
Using len() with String Indexing
When working with string indexing, it is often useful to know the length of the string. Python provides the len() function to determine the total number of characters.
text = "HELLO WORLD"
print(len(text)) # Total characters
print(text[len(text)-1]) # Last character
Output:
11
D
The expression len(text) - 1 gives the last valid index. This helps prevent IndexError when accessing characters dynamically.
Important Concept: String Immutability
Although indexing allows you to access characters, Python strings are immutable. This means you cannot change a character directly using its index.
text = "HELLO"
text[0] = "h" # Invalid operation
Output:
TypeError: 'str' object does not support item assignment
To modify a string, you must create a new one instead of changing the original.
Key Takeaways: String Indexing in Python
After exploring how Python strings are indexed, here’s a quick summary of the essential points to remember about string indexing and immutability.- Python strings use zero-based indexing, where the first character starts at index 0.
- Positive indexing moves from left to right, while negative indexing allows access from the end of the string.
- Accessing an index outside the valid range results in an
IndexError. - The
len()function helps determine the length of a string and safely access characters. - Strings in Python are immutable, meaning they cannot be changed after creation.